import { CustomTableInterface } from '@/components/ProTable/interface';
import { PublicInterface } from '@/interface/public';

import { formOpen } from '@/utils/form';
import { useDeptForm } from './use-deptForm';
import { usePositionForm } from './use-positionForm';
import { DeptInterface } from '@/api/interface/dept/dept';

export function useDataGrid(proTableBox: PublicInterface.ProTableValue) {
  const { deptForm, saveAddDeptForm, getAddDeptFormChildren } = useDeptForm(proTableBox);
  const { positionForm, saveAddPositionForm, getAddPositionFormChildren } = usePositionForm(proTableBox);
  let dataGrid = reactive<CustomTableInterface<DeptInterface.ResListByTreeByPosition>>({
    initParam: {
      id: 0
    },
    expandRowKeys: [],
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'right', width: 280 },
      {
        prop: 'name',
        label: '部门|岗位',
        width: 180,
        align: 'left'
      },

      {
        prop: 'pid',
        label: '父级'
      },
      {
        prop: 'managerId',
        label: '负责人|所属部门'
      },
      {
        prop: 'sort',
        label: '排序'
      },
      {
        prop: 'type',
        label: '类型',
        enum: statusDeptOrPosition,
        tag: true
      },
      {
        prop: 'status',
        label: '状态',
        enum: statusEnableOrDisabled,
        tag: true
      },
      {
        prop: 'remark',
        label: 'remark'
      }
    ],
    headButtonList: [
      {
        label: '新增部门',
        type: 'primary',
        icon: 'el-Plus',
        // permissionCode: 'p:auth:user:save',
        methods: async () => {
          formOpen(deptForm, '新增部门');
          getAddDeptFormChildren();
        }
      }
    ],
    rowButtonList: [
      {
        label: '编辑部门',
        type: 'primary',
        display: row => row.type === 1,
        // permissionCode: 'p:auth:user:save',
        methods: async row => {
          formOpen(deptForm, '编辑部门', row);
          getAddDeptFormChildren();
        },
        attrs: {
          link: true
        }
      },
      {
        label: '新增岗位',
        type: 'primary',
        display: row => row.type === 1,
        // permissionCode: 'p:auth:user:save',
        methods: async row => {
          formOpen(positionForm, '新增岗位', { deptId: row.id });
          getAddPositionFormChildren();
        },
        attrs: {
          link: true
        }
      },
      {
        label: '编辑岗位',
        type: 'primary',
        display: row => row.type === 2,
        // permissionCode: 'p:auth:user:save',
        methods: async row => {
          const params = {
            ...row,
            deptId: row.managerId
          };
          formOpen(positionForm, '编辑岗位', params);
          getAddPositionFormChildren();
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'enable',
        type: 'primary',
        // permissionCode: 'p:auth:user:status',
        display: item => item.status !== BasicKey.ENABLE,
        methods: row => {
          const flag = row.type === 1;
          const api = flag ? deptChangeStatus : positionChangeStatus;
          const params = { id: row.id, status: BasicKey.ENABLE };
          useHandleData(api, params, `启用【${row.name}】${flag ? '部门' : '岗位'}`, proTableBox.value?.getTableList);
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'disabled',
        type: 'danger',
        display: item => item.status !== BasicKey.DISABLE,
        methods: row => {
          const flag = row.type === 1;
          const api = flag ? deptChangeStatus : positionChangeStatus;
          const params = { id: row.id, status: BasicKey.DISABLE };
          useHandleData(api, params, `禁用【${row.name}】${flag ? '部门' : '岗位'}`, proTableBox.value?.getTableList);
        },
        attrs: {
          link: true
        }
      },
      {
        label: '删除',
        type: 'danger',
        display: row => row.status === BasicKey.ENABLE,
        methods: row => {
          const flag = row.type === 1;
          const api = flag ? deptDelete : positionDelete;
          const params = { id: row.id, status: BasicKey.DISABLE };
          useHandleData(api, params, `删除【${row.name}】${flag ? '部门' : '岗位'}`, proTableBox.value?.getTableList);
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  /**
   * 递归设置唯一标识
   */
  const setUniqueId = (data: DeptInterface.ResListByTreeByPosition[], deptId?: number) => {
    for (let item of data) {
      const { id, managerId, name } = item || {};
      item.uuid = `${id},${managerId},${name}`;
      if (item.children && item.children.length > 0) {
        setUniqueId(item.children);
      }
    }
  };
  /**
   * 对显示的数据进行回调处理
   */
  const dataCallBack = (data: DeptInterface.ResListByTreeByPosition[]) => {
    setUniqueId(data);
    return data;
  };

  return { dataGrid, dataCallBack, deptForm, saveAddDeptForm, positionForm, saveAddPositionForm };
}
